package top.tenebrous.yzl.im.observer;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;
import top.tenebrous.yzl.im.message.Message;
import top.tenebrous.yzl.im.utils.SystemInfoUtils;

import java.util.List;
import java.util.Objects;

/**
 * @author yazhang6
 */
@Component
public class ChatRecord<T extends Message> extends AbstractChatRecord<T> {

    @Qualifier("yzlImThreadPoolScheduler")
    @Autowired
    private ThreadPoolTaskExecutor taskExecutor;

    @Autowired
    private ApplicationContext applicationContext;

    @Override
    public void notifyObserver(List<T> message) {
        for(ImChatRecordHandlerProcessorObserver<T> observer : observers) {
            ObserverInfoBean bean = observerMap.get(observer);

            // 如果异步
            if (Objects.nonNull(bean) && bean.isEnableAsync()) {
                // 设置线程池
                if (StringUtils.isNotBlank(bean.getThreadPoolName())) {
                    taskExecutor = applicationContext.getBean(bean.getThreadPoolName(), bean.getThreadPoolClass());
                }
                taskExecutor.execute(() -> {
                    observer.onMessage(message);
                });

            } else {
                observer.onMessage(message);
            }

            // 本地持久化
            if (Objects.nonNull(bean) && bean.isEnableLocalStorage()) {
                // 是否为win系统
//                if (SystemInfoUtils.isWindows()) {
//
//                }
//                // 是否为mac系统
//                if (SystemInfoUtils.isMacOs()) {
//
//                }
//                // 是否为linux
//                if (SystemInfoUtils.isLinux()) {
//
//                }
//                System.out.println(JSON.toJSONString(props, true));
            }
        }
    }
}